package com.etlmaster.executor.steps.control;

import com.etlmaster.executor.utils.JsonHelper;
import com.etlmaster.executor.utils.StepState;
import com.etlmaster.executor.utils.StringUtils;

/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
public class AssignObjectProcessor extends AssignProcessor{

	@Override
	public int process(AssignStepMeta meta) {
		
		expression = expression.replaceAll("(\r\n|\r|\n|\n\r)","");
		String[] expressionArr = expression.split(";");
		for (int i = 0; i < expressionArr.length; i++) {
			int pos=expressionArr[i].indexOf("=");
			String express = expressionArr[i].substring(pos+1);
			Object value1 = null;
			if(express!=null){
				String oriValue = meta.variableSubstitution(express.trim());
				if(oriValue.startsWith("[")||oriValue.startsWith("{")){//为对象
					try{
						value1 = JsonHelper.getInstance().read(oriValue);
						
					}catch(Exception e){
						meta.log("WARN","对象赋值失败，对象格式不正确{}",oriValue);
						return StepState.RUNNING_FAILED;
					}
				}else if(StringUtils.isNumberic(oriValue)){
					value1 = StringUtils.parseNumber(oriValue);
				}else{
					value1 = oriValue.substring(1,oriValue.length()-1);//去掉引号
				}
			}
			meta.getStepState().getReturnValue().put(expressionArr[i].substring(0,pos).trim(), value1);
			meta.log("DEBUG","赋值成功 {} -> {}",expressionArr[i].substring(0,pos),value1.toString());
		}
		return StepState.RUNNING_SUCCESSED;
	}

}
